﻿using Ardalis.GuardClauses;
using HRX.PayCenter.ApplicationCore.Exceptions;
using HRX.PayCenter.ApplicationCore.Models;
using System;
using System.Collections.Generic;
using System.Text;

namespace HRX.PayCenter.ApplicationCore.Specifications
{
    /// <summary>
    /// 业务辅助类
    /// </summary>
    public static class BizHelper
    {
        /// <summary>
        /// 根据比例获取分账金额
        /// </summary>
        /// <param name="totalAmount"></param>
        /// <param name="diviseRate"></param>
        /// <returns></returns>
        public static (decimal headCompanyAmout, decimal companyAmount, decimal investorAmount) GetDiviseAmount(decimal totalAmount, string diviseRate)
        {
            //解析比例
            var rates = diviseRate.Split(new string[] { ":" }, StringSplitOptions.RemoveEmptyEntries);
            if (rates.Length <3)
            {
                Guard.Against.NullException();
            }
            decimal headCompanyRate = 0;
            decimal companyRate = 0;
            decimal investorRate = 0;

            if (!decimal.TryParse(rates[0], out headCompanyRate))
            {
                Guard.Against.NullException();
            }

            if (!decimal.TryParse(rates[1], out companyRate))
            {
                Guard.Against.NullException();
            }

            if (!decimal.TryParse(rates[2], out investorRate))
            {
                Guard.Against.NullException();
            }

            if ((headCompanyRate + companyRate + investorRate) != 10)
            {
                Guard.Against.NullException();
            }

            decimal headCompanyAmout = totalAmount * headCompanyRate / 10;
            decimal companyAmount = totalAmount * companyRate / 10;
            decimal investorAmount = totalAmount * investorRate / 10;

            return (headCompanyAmout, companyAmount, investorAmount);
        }
    }
}
